package com.octlan.asynctasklea;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

public class MainActivity extends Activity {
	ProgressDialog progressDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		createProgressDialog();
		new AsyncLoader().execute(null);
		
	}

	private void createProgressDialog() {
		progressDialog = new ProgressDialog(this);  
		  
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
		progressDialog.setMessage("Loading...");  
		progressDialog.setCancelable(false);
		progressDialog.setIndeterminate(false);  
		progressDialog.setMax(100);
		progressDialog.show();
	}

	class AsyncLoader extends AsyncTask<Void, Integer, Integer> {

		@Override
		protected Integer doInBackground(Void... params) {
			try {
				for (int i = 0; i < 100; i++) {
					System.out.println("***********" + i + "*************");
					Thread.currentThread().sleep(1000);
					publishProgress(i);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			//Intent intent = new Intent(MainActivity.this, InformationActivity.class);
			Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			progressDialog.setProgress(values[0]);
		}

	}
}
